﻿/*  ----------------------------------------------------------------------------
 *  EcoSqlCondition (http://ecosqlcondition.codeplex.com)
 *  ----------------------------------------------------------------------------
 *  License:    http://ecosqlcondition.codeplex.com/license  
 *  ----------------------------------------------------------------------------
 *  Company:    P3-Studio (http://www.p3studio.pl)
 *  File:       SqlCondition.cs
 *  Author:     Paweł Pawłow
 *  ----------------------------------------------------------------------------
 */

using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Eco.DataRepresentation;
using Eco.Ocl.Impl;
using Eco.ObjectRepresentation;
using Eco.Services;


namespace Eco.Persistence
{
	[Serializable, DataContract, KnownType(typeof(ObjectId)), KnownType(typeof(ObjectIdList))]
	public class SqlCondition : AbstractCondition
	{
		public SqlCondition(IEcoServiceProvider serviceProvider, Type classType)
			: this(serviceProvider.GetEcoService<ITypeSystemService>().TypeSystem.GetClassByType(classType).InternalIndex)
		{
		}

		public SqlCondition(IEcoServiceProvider serviceProvider, Type classType, int version)
			: this(serviceProvider.GetEcoService<ITypeSystemService>().TypeSystem.GetClassByType(classType).InternalIndex, version)
		{
		}

		public SqlCondition()
			: this(0)
		{
		}

		public SqlCondition(int classId)
			: this(classId, 0x7fffffff)
		{
		}

		public SqlCondition(int classId, int version)
			: base(classId, version)
		{
			Env = new List<TBoldOLWVariableBinding>();
		}

		public SqlCondition(int classId, string sql)
			: this(classId)
		{
			Sql = sql;
		}

		/// <summary>
		/// Start with select eco_id, eco_type from ...
		/// </summary>
		[DataMember]
		public string Sql { get; set; }

		[DataMember]
		public List<TBoldOLWVariableBinding> Env { get; set; }

		public OclCondition.IExternalVariable GetExternalVariable(int index)
		{
			return Env[index];
		}
	}
}
